package com.tarena.lbs.marketing.web.scheduled;

import com.tarena.lbs.marketing.web.service.MarketingActivityService;
import com.tarena.lbs.common.marketing.enums.ActivityStatusEnum;
import com.tarena.lbs.pojo.marketing.bo.MarketingActivityBO;
import com.tarena.lbs.pojo.marketing.param.UpdateParam;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;

@Configuration
public class ActivityScheduled {

    @Resource
    private MarketingActivityService marketingActivityService;

//    @Scheduled(cron="")
    public void updateActivityStatus() {
        //暂时不考虑分页处理
        Date now = new Date();
        List<MarketingActivityBO> list = marketingActivityService.selectListByStatus();
        if (!CollectionUtils.isEmpty(list)) {
            List<UpdateParam> params = new ArrayList<>();
            for (MarketingActivityBO marketingActivityBO : list) {
                if (marketingActivityBO.getEndDate().compareTo(now) < 0) {
                    marketingActivityBO.setStatus(ActivityStatusEnum.DELETED.getCode());
                    params.add(new UpdateParam(marketingActivityBO.getStatus(), marketingActivityBO.getId()));
                }
                if (marketingActivityBO.getStartDate().compareTo(now) < 0 && marketingActivityBO.getEndDate().compareTo(now) > 0) {
                    marketingActivityBO.setStatus(ActivityStatusEnum.ENABLE.getCode());
                    params.add(new UpdateParam(marketingActivityBO.getStatus(), marketingActivityBO.getId()));
                }
            }
            if (!CollectionUtils.isEmpty(params)) {
                marketingActivityService.batchUpdateActivityStatus(params);
            }
        }
    }
}
